/*
 * @Author: CanEve
 * @Date: 2023-07-20 02:26:41
 * @LastEditTime: 2023-07-20 02:42:35
 * @FilePath: /oled/oled.c
 * @Description: 
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
unsigned int fd;
void oled_cmd(unsigned char cmd){
    unsigned char buf_cmd[3];
    write(fd, &buf_cmd, 3);
}
void oled_init(){
    // unsigned char buf[3];
    // buf[0] = 0x01;
    // buf[1] = 0x00;
    // buf[2] = 0x01;
   oled_cmd(0xae);
}

int main(int argc, char **argv)
{

unsigned char buf[16];

fd = open("/dev/i2c-0", O_RDWR);
if (!fd) {
printf("open device failed!\n");
return -1;
}
ioctl(fd, I2C_SLAVE, 0x3c); //slave address
ioctl(fd, I2C_TIMEOUT, 1); //timeout
ioctl(fd, I2C_RETRIES, 1); //try count

//write data to i2c-1
buf[0] = 0x01;
buf[1] = 0x00;
buf[2] = 0x01;
write(fd, &buf[0], 3);

//read data from i2c-1
buf[0] = 0x01;
buf[1] = 0x00;
write(fd, &buf[0], 2); //write register address first
buf[2] = 0x00;
read(fd, &buf[2], 1); //read data

printf("%d\n", buf[2]);

close(fd);

return 0;
}